package com.nhjf.mobile.controller.jobDetails;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.nhjf.mobile.common.util.SysProperties;
import com.nhjf.model.jobDetails.JobDetails;
import com.nhjf.model.student.Student;
import com.nhjf.model.system.AdminSession;
import com.nhjf.model.system.User;
import com.nhjf.pojo.dto.result.ServiceResultDto;
import com.nhjf.pojo.qo.jobDetails.JobDetailsQo;
import com.nhjf.pojo.qo.studnet.StudentQo;
import com.nhjf.pojo.qo.system.AdminSessionQo;
import com.nhjf.pojo.qo.system.UserQo;
import com.nhjf.service.jobDetails.JobDetailsService;
import com.nhjf.service.student.StudentService;
import com.nhjf.service.system.AdminSessionService;
import com.nhjf.service.system.UserService;

import net.sf.json.JSONObject;

@RequestMapping("/jobDetails")
@Controller
public class JobDetailsController {
	@Autowired
	private JobDetailsService detailsService;
	@Autowired
	private AdminSessionService adminSessionService;
	@Autowired
	private UserService userService;
	@Autowired
	private StudentService studentService; 
	@RequestMapping("/jobDetails_post")
	@ResponseBody
	public String jobDetails_Post(@RequestParam("file") MultipartFile file,@RequestParam(value="token", required=true)String token,String jobDetailsId) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		AdminSessionQo adminSessionQo=new AdminSessionQo();
		adminSessionQo.setToken(token);
		AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
		if(adminSession==null) {
			resMap.put("code", "406");
			resMap.put("message", "不存在此账号！");
			return JSONObject.fromObject(resMap).toString();
		}else {
			UserQo userQo=new UserQo();
			userQo.setUserName(adminSession.getLoginName());
			User user= userService.queryUnique(userQo);
			if(user==null) {
				resMap.put("code", "406");
				resMap.put("message", "未登录!");
				return JSONObject.fromObject(resMap).toString();
			}else {
				String originalFilename = file.getOriginalFilename();
				//文件名使用当前时间
				String date = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
				// 获取上传图片的扩展名(jpg/png/...)
				String extension = FilenameUtils.getExtension(originalFilename);
				String[] regular = { "JPG", "GIF","PNG","JPEG" ,"BMP"};
				boolean flag = false;
				// 判断文件格式是否合法
				for (String str : regular) {
					if (extension.equalsIgnoreCase(str)) {
						flag = true;
					}
				}
				if (!flag) {
					resMap.put("code", "500");
					resMap.put("message", "上传图片格式不正确！");
					return JSONObject.fromObject(resMap).toString();
				}

				SysProperties sysProperties=SysProperties.getInstance();
				String operation=sysProperties.get("operation");
				// 图片上传的相对路径（因为相对路径放到页面上就可以显示图片）

				String path =operation+user.getId()+"/" + date + "." + extension;
				//图片上传的绝对路径

				File dir = new File(path);
				if (!dir.exists()) {
					dir.mkdirs();
				}
				// 上传图片
					try {
						file.transferTo(new File(path));
						JobDetailsQo jobDetailsQo=new JobDetailsQo();
						jobDetailsQo.setSeachPostJob(true);
						jobDetailsQo.setSeachStudent(true);
						jobDetailsQo.setJobDetailsId(Long.parseLong(jobDetailsId));
						JobDetails details= detailsService.queryUnique(jobDetailsQo);
						details.setNotchangedURL(user.getId()+"/"+ date + "." + extension);
						details.setReleasetime(new Date());
						details.setStatus(1);
						JobDetails details2= detailsService.update(details);
						if(details2!=null) {
							resMap.put("code", "200");
							resMap.put("message", "成功");
							return JSONObject.fromObject(resMap).toString();
						}
						resMap.put("code", "500");
						resMap.put("message", "操作失败");
						return JSONObject.fromObject(resMap).toString();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			}
		}
		resMap.put("code", "406");
		resMap.put("message", "未登录!");
		return JSONObject.fromObject(resMap).toString();
		
	}
}
